package com.mycjj.android.util;

import java.util.ArrayList;

import com.mycjj.android.MyCjjApplication;
import com.mycjj.android.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class LoadingBar extends RelativeLayout{

//	private Context context;
	private Thread animationThread;
	private boolean stopped = true;
	private int currentStep = 0;
	private int count = 7;
	private ImageView logoIv;
	private float dotheight ;
	private float progresswidth ;
	private float topMaigin;
	private float progressLeft;
	private float progressTop;
	
	public LoadingBar(Context context) {
		super(context.getApplicationContext());
//		this.context = context.getApplicationContext();
		this.setEnabled(false);
		this.setBackgroundColor(Color.BLACK);
		
		progresswidth = MyCjjApplication.getInstance().getDisplayHightAndWightPx()[1]/5.3f;
		dotheight = MyCjjApplication.getInstance().getDisplayHightAndWightPx()[0]/42.7f;
		topMaigin = MyCjjApplication.getInstance().getDisplayHightAndWightPx()[0]/26.7f;
		prepareLayout();
		
	}
	
	public LoadingBar(Context context, AttributeSet attrs) {
	    super(context, attrs);
	}
	
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    	return false;
    }
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent pEvent) {
		return false;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}
	
    @Override 
    protected void onDraw(Canvas canvas) {
    	
    	for(int i=0;i<count;i++){
    		this.drawRect(canvas, i);
    	}
    	invalidate();
    }
    
    private void drawRect(Canvas canvas,int index){
  	  Paint paint = new Paint();
  	  paint.setColor(this.getColor(index));
  	  paint.setStyle( Paint.Style.FILL );
  	  
	  progressLeft = getWidth()/2f - progresswidth/2f;
	  progressTop = getHeight()/2f + topMaigin;
		
  	  float left ;
  	  float top ;
  	  float right ;
  	  float bottom ;
  	  
      if (currentStep == index) {
    	  left =progressLeft + progresswidth*index/8.0f;
    	  top = progressTop;
    	  right = left + progresswidth/(count*2+1);
    	  bottom = top + dotheight;
      } else if (currentStep < index) {
    	  left =progressLeft + progresswidth*index/8.0f;
    	  top = progressTop + dotheight/5.0f;
    	  right = left + progresswidth/(count*2+1);
    	  bottom = top + dotheight*3.0f/5.0f;
      } else {
          if (currentStep - index == 1) {
        	  left = progressLeft + progresswidth*index/8.0f;
        	  top = progressTop + dotheight/10.0f;
        	  right = left + progresswidth/(count*2+1);
        	  bottom = top + dotheight*4.0f/5.0f;
        	  
          } else {
        	  left = progressLeft + progresswidth*index/8.0f;
        	  top = progressTop + dotheight/5.0f;
        	  right = left + progresswidth/(count*2+1);
        	  bottom = top + dotheight*3.0f/5.0f;
          }
      }
  	  canvas.drawRect( left,top,right,bottom, paint );
    }
    
    private int getColor(int index){
        if (currentStep == index) {
        	return Color.rgb(0, 150, 200);
        } else if (currentStep < index) {
            return Color.alpha(0);
        } else {
            if (currentStep - index == 1) {
            	return Color.rgb(0, 150, 200);
            } else {
            	return Color.rgb(255, 255, 255);
            }
        }        
    }

	/**
	 * This is called when you want the dialog to be dismissed
	 */
	public void dismiss() {
		stopped = false;
		if(animationThread != null ){
			animationThread.interrupt();
	    }
//		setVisibility(View.GONE);
	}

	/**
	 * Loads the layout and sets the initial set of images
	 */
	private void prepareLayout() {

		logoIv = new ImageView(this.getContext());
		logoIv.setImageResource(R.drawable.car_loading_icon);// project_logo 图片

		logoIv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		addView(logoIv,new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		this.setGravity(Gravity.CENTER);
	}

	/**
	 * Starts the animation thread
	 */
	public void startAnimation() {
		setVisibility(View.VISIBLE);
		animationThread = new Thread(new Runnable(){
			@Override
			public void run() {
				while (stopped) {
					try {
						Log.d("", "---------------");
						Thread.sleep(50);
						handler.sendEmptyMessage(0);
					} catch (InterruptedException e) {
						e.printStackTrace();
						Thread.currentThread().interrupt();
					}
				}				
			}}, "Progress");
		animationThread.start();
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			 currentStep = ++currentStep % (count*2+1);
			super.handleMessage(msg);
		}

	};

}